package com.ts.api.module.api.convert.market.http;

import com.gitee.apanlh.util.log.Log;
import com.gitee.apanlh.util.reflection.CopyUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.common.constant.api.ApiBizTypeEnum;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.http.client.ApiHttpExecutor;
import com.ts.api.module.api.convert.core.http.client.ApiHttpMessage;
import com.ts.api.module.api.convert.core.http.client.ApiHttpRequestConfigure;
import com.ts.api.module.api.convert.market.context.ApiConsumerMarketConvertContext;
import com.ts.api.module.api.convert.market.handler.ConsumerMarketConvertHandler;
import com.ts.api.module.api.entity.ApiUserRequest;
import com.ts.api.module.channel.entity.po.ChannelMarketPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *  下游-贷超-HTTP
 *
 *  @author Pan
 */
@Component
public class ApiConsumerMarketHttpHandler {

    @Autowired
    private ApiHttpExecutor apiHttpExecutor;
    @Autowired
    private ApiConsumerMarketConvertContext apiConsumerMarketConvertContext;
    @Autowired
    private ApiHttpRequestConfigure apiHttpRequestConfigure;

    /**
     *  发送贷超撞库
     *
     *  @param  channelMarketPO     贷超实体
     *  @param  apiUserRequest         请求对象
     *  @param  httpTimeout         超时时长
     *  @return ApiHttpMessage
     */
    public ApiHttpMessage sendDataExchange(ChannelMarketPO channelMarketPO, ApiUserRequest apiUserRequest, Integer httpTimeout) {
        ApiHttpMessage httpMessage = ApiHttpMessage.create(channelMarketPO.getApiDataExchangeUrl(), ApiBizTypeEnum.DATA_EXCHANGE);

        try {
            String reqId = apiUserRequest.getReqId();

            //  获取对应计划撞库发送实体
            ConsumerMarketConvertHandler consumerMatcherHandler = apiConsumerMarketConvertContext.getConsumerDataExchangeHandler(channelMarketPO);
            //  将ApiUser信息转化发送HTTP消息实体
            ApiDataExchangeParameter apiDataExchangeParameter = CopyUtils.copy(apiUserRequest, ApiDataExchangeParameter.class);
            //  获取发送请求体
            Object sendBody = consumerMatcherHandler.dataExchange(apiDataExchangeParameter);

            //  拓展属性装配
            apiHttpRequestConfigure.apiExtendConfigure(httpMessage, apiDataExchangeParameter, sendBody, reqId, httpTimeout - 200);
            httpMessage.setPhoneMd5(apiDataExchangeParameter.getPhoneMd5());

            //  发送API请求
            apiHttpExecutor.execute(httpMessage);

            String responseBody = httpMessage.getResponseBody();
            //  验证响应非空情况
            if (ValidParam.isEmpty(responseBody) || ValidParam.isNotEmpty(httpMessage.getErrorMsg()) || httpMessage.hasReqTimeout()) {
                httpMessage.setHasSuc(false);
                return httpMessage;
            }
            //  响应验证
            boolean hasDataExchangeSuc = consumerMatcherHandler.hasDataExchangeSuc(responseBody);
            httpMessage.setHasSuc(hasDataExchangeSuc);
            httpMessage.setResponseBody(responseBody);
            return httpMessage;
        }  catch (Exception e) {
            Log.get().error("{}", e.getMessage(), e);
            httpMessage.setHasSuc(false);
            httpMessage.setExceptionInfo(e);
            return httpMessage;
        }
    }

    /**
     *  发送联登
     *
     *  @param  channelMarketPO   渠道贷超实体
     *  @param  apiUserRequest       请求参数
     *  @param  httpTimeout       超时时长
     *  @return ApiHttpMessage
     */
    public ApiHttpMessage sendFederate(ChannelMarketPO channelMarketPO, ApiUserRequest apiUserRequest, Integer httpTimeout) {
        ApiHttpMessage httpMessage = ApiHttpMessage.create(channelMarketPO.getApiFederateUrl(), ApiBizTypeEnum.APPLY);

        try {
            String reqId = apiUserRequest.getReqId();

            //  获取对应计划撞库发送实体
            ConsumerMarketConvertHandler consumerMatcherHandler = apiConsumerMarketConvertContext.getConsumerFederateHandler(channelMarketPO);
            //  将ApiUser信息转化发送HTTP消息实体
            ApiApplyParameter apiApplyParameter = CopyUtils.copy(apiUserRequest, ApiApplyParameter.class);

            //  获取发送请求体
            Object sendBody = consumerMatcherHandler.federate(apiApplyParameter);

            //  拓展属性装配
            apiHttpRequestConfigure.apiExtendConfigure(httpMessage, apiApplyParameter, sendBody, reqId, httpTimeout - 200);

            //  发送API请求
            apiHttpExecutor.execute(httpMessage);
            String responseBody = httpMessage.getResponseBody();

            //  验证响应非空情况
            if (ValidParam.isEmpty(responseBody) || ValidParam.isNotEmpty(httpMessage.getErrorMsg()) || httpMessage.hasReqTimeout()) {
                httpMessage.setHasSuc(false);
                return httpMessage;
            }

            //  响应验证
            boolean hasSucFederate = consumerMatcherHandler.hasSucFederate(responseBody);
            httpMessage.setHasSuc(hasSucFederate);
            httpMessage.setResponseBody(responseBody);
            return httpMessage;
        }  catch (Exception e) {
            Log.get().error("{}", e.getMessage(), e);
            httpMessage.setHasSuc(false);
            httpMessage.setExceptionInfo(e);
            return httpMessage;
        }
    }
}
